<?php

namespace app\common\validate;

use think\Validate;

class TeacherAudit extends Validate
{
    /**
     * 验证规则
     */
    protected $rule = [
        'user_id|用户' => 'require',
        'teaching_method|授课方式' => 'require',
        'settled_type_free|免费入驻类型' => 'requireIf:settled_type,',
        'settled_type|付费入驻类型' => 'requireIf:settled_type_free,',
        'nickname|昵称' => 'require|length:1,4|different:realname',
        'mobile|联系方式' => 'require|mobile',
        'wx_phone|微信手机号' => 'require|mobile',
        'realname|真实姓名' => 'require|chs|length:1,5',
        'id_code|身份证号' => 'IdCard',
        'sex|性别' => 'require|in:男,女',
        'id_card_front|身份证正面' => 'imageSingle',
        'id_card_reverse|身份证反面' => 'imageSingle',
        'school_image|学生证/校园卡/毕业证书' => 'require|imageSingle',
        'school_image_hand|手持学生证/校园卡/毕业证书' => 'require|imageSingle',
        'school_name|院校名称' => 'require|length:3,20',
        'learn_start|学历开始时间' => 'date|beforeWith:learn_end',
        'learn_end|学历结束时间' => 'date|afterWith:learn_start',
        'education|学历' => 'require|length:2,8',
        'profession|专业' => 'require|length:2,20',
        'grade|年级' => 'require',
        'give_lessons|授课科目' => 'require|giveLessonsRule',
        'give_timesec|常年授课概况' => 'require',
        'give_time|周授课空闲时间' => 'require',
        'unholiday_pick|非寒暑假授课省份' => 'require',
        'unholiday_area|非寒暑假授课区域' => 'require|holidayRule',
        'unholiday_areas|寒暑假授课区域' => 'require|array|length:1,40',
        'holiday_pick|寒暑假授课省份' => 'require',
        'holiday_area|寒暑假授课区域' => 'require|holidayRule',
        'holiday_areas|寒暑假授课区域' => 'require|array|length:1,40',
        'photo_images|个人照片' => 'array|length:1,9',
        'avatar|头像' => 'imageSingle',
        'teacher_age|教龄' => 'number|between:1,50',
        'tags|个人标签' => 'require|array|length:1,3',
        'main_image|教师资格证' => 'imageSingle',
        'cert_name|职称证书名称' => 'requireWith:cert_image|length:2,20|chs',
        'cert_image|职称证书' => 'requireWith:cert_name|imageSingle',
        'teacher_exp|教学经历' => 'array|teacherExpDateRule|teacherExpLengthRule|teacherExpDateCompareRule',
        'case_name|成功案例标题' => 'requireWith:case_content|length:2,20|chs',
        'case_content|教学成效、科研成果' => 'requireWith:case_name|length:50,200',
        'honor_images|奖励荣誉' => 'array',
        'person_video|展示视频' => 'videoSingle',
    ];
    /**
     * 提示消息
     */
    protected $message = [
        'user_id.require' => '用户不能为空',
        'teaching_method.require' => '1.授课方式不能为空',
        'settled_type_free.require' => '2.免费入驻类型不能为空',
        'settled_type_free.requireIf' => '2.入驻类型最少要选择一项',
        'settled_type.require' => '2.付费入驻类型不能为空',
        'settled_type.requireIf' => '2.入驻类型最少要选择一项',
        'nickname.require' => '3.昵称不能为空',
        'nickname.length' => '3.昵称的长度在1-4之间',
        'nickname.different' => '3.昵称不可和真实姓名一样',
        'mobile.require' => '3.联系方式不能为空',
        'wx_phone.require' => '3.微信手机号不能为空',
        'realname.require' => '3.真实姓名不能为空',
        'realname.length' => '3.真实姓名的长度在1-8之间',
        'id_code.require' => '3.身份证号不能为空',
        'sex.require' => '3.性别不能为空',
        'id_card_front.require' => '4.身份证正面不能为空',
        'id_card_reverse.require' => '4.身份证反面不能为空',
        'school_image.require' => '5.学生证/校园卡/毕业证书不能为空',
        'school_image_hand.require' => '5.手持学生证/校园卡/毕业证书不能为空',
        'school_name.require' => '6.院校名称不能为空',
        'school_name.length' => '6.院校名称的长度在3-20之间',
        'learn_start.require' => '6.学历开始时间不能为空',
        'learn_start.beforeWith' => '6.学历开始时间不能在结束时间之后',
        'learn_end.require' => '6.学历结束时间不能为空',
        'learn_end.afterWith' => '6.学历结束时间不能在开始时间之前',
        'education.require' => '6.学历不能为空',
        'education.length' => '6.学历的长度在2-8之间',
        'profession.require' => '6.专业不能为空',
        'profession.length' => '6.专业的长度在2-20之间',
        'grade.require' => '6.年级不能为空',
        'give_lessons.require' => '7.授课科目不能为空',
        'give_lessons.giveLessonsRule' => '7.请完善授课科目',
        'give_timesec.require' => '8.常年授课概况不能为空',
        'give_time.require' => '8.周授课空闲时间不能为空',
        'unholiday_area.require' => '9.非寒暑假授课区域不能为空',
        'unholiday_area.holidayRule' => '9.非寒暑假授课区域请选择地址',
        'unholiday_areas.require' => '9.非寒暑假授课区域不能为空',
        'unholiday_areas.length' => '9.非寒暑假授课区域至少要选择一个',
        'unholiday_pick.require' => '9.请选择非寒暑假授课省份',
        'holiday_pick.require' => '10.请选择寒暑假授课省份',
        'holiday_area.require' => '10.寒暑假授课区域不能为空',
        'holiday_area.holidayRule' => '10.寒暑假授课区域请选择地址',
        'holiday_areas.require' => '10.寒暑假授课区域不能为空',
        'holiday_areas.length' => '10.寒暑假授课区域至少要选择一个',
        'photo_images.require' => '11.个人照片不能为空',
        'photo_images.length' => '11.个人照片至少上传1张，最多上传9张',
        'avatar.require' => '12.头像不能为空',
        'teacher_age.require' => '13.教龄不能为空',
        'teacher_age.between' => '13.教龄最小为1最大为50',
        'tags.require' => '14.个人标签不能为空',
        'tags.length' => '14.个人标签长度最多为3个',
        'main_image.require' => '15.教师资格证不能为空',
        'cert_name.require' => '16.职称证书名称不能为空',
        'cert_name.requireWith' => '16.请适当填写职称证书名称',
        'cert_image.require' => '16.职称证书不能为空',
        'cert_image.requireWith' => '16.请适当上传职称证书',
        'teacher_exp.require' => '17.教学经历不能为空',
        'teacher_exp.teacherExpDateRule' => '17.请完善教学经历',
        'teacher_exp.teacherExpLengthRule' => '17.教学经历内容长度至少在50字以上',
        'teacher_exp.teacherExpDateCompareRule' => '17.教学经历开始时间必须在结束时间之前',
        'case_name.requireWith' => '18.请适当填写成功案例标题',
        'case_name.length' => '18.成功案例长度在2-20之间',
        'case_content.requireWith' => '18.请适当填写教学成效、科研成果',
        'case_content.length' => '18.教学成效、科研成果长度在50-200之间',
        'honor_images.require' => '19.奖励荣誉不能为空',
        'person_video.require' => '20.展示视频不能为空',
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => [
            'user_id',
            'teaching_method',
            'settled_type_free',
            'settled_type',
            'nickname',
            'mobile',
            'wx_phone',
            'realname',
            'id_code',
            'sex',
            'id_card_front',
            'id_card_reverse',
            'school_image',
            'school_image_hand',
            'school_name',
            'learn_start',
            'learn_end',
            'education',
            'profession',
            'grade',
            'give_lessons',
            'give_timesec',
            'give_time',
            'unholiday_area',
            'unholiday_areas',
            'holiday_area',
            'holiday_areas',
            'photo_images',
            'avatar',
            'teacher_age',
            'tags',
            'main_image',
            'cert_name',
            'cert_image',
            'teacher_exp',
            'case_name',
            'case_content',
            'honor_images',
            'person_video',
        ],
        'edit' => [],
    ];


    function IdCard($idCard)
    {
        //15位和18位身份证号码的正则表达式
        $_regIdCard = '/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$/';

        //如果通过该验证，说明身份证格式正确，但准确性还需计算
        //if($_regIdCard.test(idCard)){
        if (!preg_match($_regIdCard, $idCard)) {
            return FALSE;
        }


        if (strlen($idCard) !== 18) {
            return FALSE;
        }

        $_idCardWi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; //将前17位加权因子保存在数组里
        $_idCardY = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2]; //这是除以11后，可能产生的11位余数、验证码，也保存成数组
        $_idCardWiSum = 0; //用来保存前17位各自乖以加权因子后的总和

        for ($i = 0; $i < 17; $i++) {
            $_idCardWiSum += substr($idCard, $i, 1) * $_idCardWi[$i];
        }

        $_idCardMod = $_idCardWiSum % 11;//计算出校验码所在数组的位置

        $_idCardLast = substr($idCard, -1);//得到最后一位身份证号码

        //如果等于2，则说明校验码是10，身份证号码最后一位应该是X
        if ($_idCardMod === 2) {
            if ($_idCardLast === "X" || $_idCardLast === "x") {
                return TRUE;
            }
        } else {
            //用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
            if ($_idCardLast == $_idCardY[$_idCardMod]) {
                return TRUE;
            }
        }

        return FALSE;
    }

    function mobile($mobile)
    {
        $pattern = "/^1[3-9]\d{9}$/";
        if (!preg_match($pattern, $mobile)) {
            return FALSE;
        }
        return TRUE;
    }

    function imageSingle($image)
    {
        $pattern = "/^\/uploads\/\d{8}\/.*?\.(jpg|jpeg|png|gif)$/";
        if (!preg_match($pattern, $image)) {
            return FALSE;
        }
        return TRUE;
    }

    function videoSingle($image)
    {
        $pattern = "/^\/uploads\/\d{8}\/.*?\.(mp4|avi)$/";
        if (!preg_match($pattern, $image)) {
            return FALSE;
        }
        return TRUE;
    }

    function giveLessonsRule($value){
        //主教必须全有
        if($value[0]['grade'] && $value[0]['subject']){

        }else{
            return FALSE;
        }

        if(isset($value[1])){
            //辅教要有就全都有
            if($value[1]['grade'] || $value[1]['subject']){
                if($value[1]['grade'] && $value[1]['subject']){

                }else{
                    return FALSE;
                }
            }
        }
        if(isset($value[2])){
            //辅教要有就全都有
            if($value[2]['grade'] || $value[2]['subject']){
                if($value[2]['grade'] && $value[2]['subject']){

                }else{
                    return FALSE;
                }
            }
        }
        return TRUE;
    }

    function teacherExpDateRule($value)
    {
        foreach ($value as $k => $v) {
            if($value[$k]['start'] || $value[$k]['end'] || $value[$k]['content']) {
                if ($value[$k]['start'] && $value[$k]['end'] && $value[$k]['content']) {
                    if (!preg_match("/^(19|20)\d{2}\-\d{2}\-\d{2}$/", $value[$k]['start'])) {
                        return FALSE;
                    }
                    if (!preg_match("/^(19|20)\d{2}\-\d{2}\-\d{2}$/", $value[$k]['end'])) {
                        return FALSE;
                    }
                } else {
                    return FALSE;
                }
            }
        }
        return TRUE;
    }

    function teacherExpDateCompareRule($value)
    {
        foreach ($value as $k => $v) {
            if($value[$k]['start'] || $value[$k]['end'] || $value[$k]['content']) {
                if ($value[$k]['start'] && $value[$k]['end']) {
                    if (strtotime($value[$k]['start']) > strtotime($value[$k]['end'])) {
                        return FALSE;
                    }
                } else {
                    return FALSE;
                }
            }
        }
        return TRUE;
    }

    function teacherExpLengthRule($value)
    {
        foreach ($value as $k => $v) {
            if($value[$k]['start'] || $value[$k]['end'] || $value[$k]['content']) {
                $length = mb_strlen($value[$k]['content'], 'UTF-8');
                if ($length < 50) {
                    return FALSE;
                }
            }
        }
        return TRUE;
    }

    public function holidayRule($value)
    {
        if(!$value[0] || !$value[1] || !$value[2] || !$value[3]){
            return FALSE;
        }
        return TRUE;
    }

}
